package com.ruoyi.common.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtils {

    /**
     * 加密
     *
     * @param strKey 密钥
     * @param strIn  待加密串
     * @return * @throws Exception
     */
    public static String encrypt(String strKey, String strIn) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec("0000000000000000".getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(strKey.getBytes(), "AES"), iv);
            byte[] encrypted = cipher.doFinal(strIn.getBytes());
            System.out.println("new BASE64Encoder().encode(encrypted) = " + new BASE64Encoder().encode(encrypted));
            return new BASE64Encoder().encode(encrypted);
        } catch (Exception e) {
            throw new RuntimeException("加密失败", e);
        }
    }

    /**
     * 解密
     *
     * @param strKey 密钥
     * @param strIn  待加密串
     * @return
     */
    public static String decrypt(String strKey, String strIn) {
        try {
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(strIn);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keyspec = new SecretKeySpec(strKey.getBytes(), "AES");
            IvParameterSpec iv = new IvParameterSpec("0000000000000000".getBytes());
            cipher.init(Cipher.DECRYPT_MODE, keyspec, iv);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        } catch (Exception e) {
            throw new RuntimeException("解密失败", e);
        }
    }

    public static void main(String aregs[]) {
        String str_test = "胡汉三";
        String key = "mj/Pc8haRoYUFcxG2aFVcQ==";
        try {
            System.out.println("加密前" + str_test);
            String encrypt = AESUtils.encrypt(key, str_test);
            System.out.println("加密后" + encrypt);
            String decrypt_word = AESUtils.decrypt(key, encrypt);
            System.out.println("解密后" + decrypt_word);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    /**
     * 生成加密key方法
     * @param args
     */
//        public static void main(String[] args) {
//            try {
//                // 创建一个AES的KeyGenerator实例
//                KeyGenerator keyGen = KeyGenerator.getInstance("AES");
//
//                // 初始化KeyGenerator，指定密钥长度（128, 192, 或 256位）
//                keyGen.init(128); // 256位密钥
//
//                // 生成密钥
//                SecretKey secretKey = keyGen.generateKey();
//
//                // 获取密钥的字节数组
//                byte[] keyBytes = secretKey.getEncoded();
//
//                // 将密钥转换为Base64格式的字符串，方便输出
//                String encodedKey = Base64.getEncoder().encodeToString(keyBytes);
//
//                // 输出密钥
//                System.out.println("Generated AES Key (Base64): " + encodedKey);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }


}